package config

import (
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"os"
	"path/filepath"
	"web-app/pkg/db"
)

type Config struct {
	Server ServerConfig `yaml:"server"`
	DB     db.Config    `yaml:"DB"`
}

type ServerConfig struct {
	Port    int    `yaml:"Port"`
	Address string `yaml:"Address"`
}

func NewConfig(path string) (*Config, error) {
	currentDir, err := os.Getwd()
	if err != nil {
		return nil, err
	}
	absolutePath := filepath.Join(currentDir, path)
	content, err := ioutil.ReadFile(absolutePath)
	if err != nil {
		return nil, err
	}
	config := &Config{}
	if yaml.Unmarshal(content, config) != nil {
		return nil, err
	}
	return config, nil
}
